package cn.zc.curriculum.service.service.impl;


import cn.tools.core.exception.BizException;
import cn.zc.curriculum.entity.MenuAndQuestion;
import cn.zc.curriculum.entity.VO.MenuAndQuestionVO;
import cn.zc.curriculum.service.mapper.MenuAndQuestionMapper;
import cn.zc.curriculum.service.service.MenuAndQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 目录对应的题库 服务实现类
 * </p>
 *
 * @author keno
 * @since 2022-02-14
 */
@Service
public class MenuAndQuestionServiceImpl extends ServiceImpl<MenuAndQuestionMapper, MenuAndQuestion> implements MenuAndQuestionService {

    @Resource
    private MenuAndQuestionMapper menuAndQuestionMapper;

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void createMenuAndQuestion(MenuAndQuestionVO menuAndQuestionVO) {
        List<MenuAndQuestion> list = new ArrayList<>();
        for (String qbId:menuAndQuestionVO.getQuestionId()) {
            MenuAndQuestion menuAndQuestion = new MenuAndQuestion();
            menuAndQuestion.setMenuId(menuAndQuestionVO.getMenuId());
            menuAndQuestion.setQuestionId(qbId);
            list.add(menuAndQuestion);
        }
        if(!saveBatch(list)){
            throw new BizException("插入失败");
        }
    }


    @Override
    public void deleteMenuAndQuestion(String menuId) {
        menuAndQuestionMapper.delete(
        new QueryWrapper<MenuAndQuestion>().lambda()
                .eq(MenuAndQuestion::getMenuId,menuId));
    }
}
